package com.hao.wms.mvc;

import com.hao.wms.domain.Product;
import com.hao.wms.query.ProductQueryObject;
import com.hao.wms.service.IBrandService;
import com.hao.wms.service.IProductService;
import com.hao.wms.util.FileUploadUtil;
import com.hao.wms.util.RequiredPermission;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import java.io.File;
import java.io.IOException;

public class ProductAction extends BaseAction{
	/**
	 * 
	 */
	private IProductService productService;
	private IBrandService brandService;
	
	private Product product = new Product();
	private File pic;
	private String picFileName;

	public File getPic() {
		return pic;
	}

	public void setPic(File pic) {
		this.pic = pic;
	}

	public String getPicFileName() {
		return picFileName;
	}

	public void setPicFileName(String picFileName) {
		this.picFileName = picFileName;
	}

	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}

	public void setBrandService(IBrandService brandService) {
		this.brandService = brandService;
	}

	private ProductQueryObject queryObject = new ProductQueryObject();
	
	
	public ProductQueryObject getQueryObject() {
		return queryObject;
	}
	public void setQueryObject(ProductQueryObject queryObject) {
		this.queryObject = queryObject;
	}
	public void setProductService(IProductService productService) {
		this.productService = productService;
	}
	@InputConfig(methodName = "input")
	@RequiredPermission(value="产品列表")
	public String execute() {
		putContext("brands",brandService.list());
		putContext(PAGERESULT, productService.query(queryObject));
		
		return LIST;
	}
	@RequiredPermission(value="新增/修改产品")
	public String input() {
		putContext("brands",brandService.list());
		if(product.getId()!=null) {
			product = productService.get(product.getId());
		}
		return INPUT;
	}
	@RequiredPermission(value="修改产品")
	public String saveOrUpdate() throws Exception {
		if(product.getId()!=null&&pic!=null&&product.getImagePath()!=null){
			FileUploadUtil.deleteFile(product.getImagePath());
		}
		if(pic!=null){
			String filePath = FileUploadUtil.uploadFile(pic,picFileName);
			product.setImagePath(filePath);
		}
		try {
			if(product.getId()!=null) {
                productService.update(product);
                addActionMessage("修改成功");
            }else {
                productService.save(product);
                addActionMessage("保存成功");
            }
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	public void prepareSaveOrUpdate(){
		if(product.getId()!=null) {
			product = productService.get(product.getId());
			product.setBrand(null);
		}
	}
	@RequiredPermission(value="删除产品")
	public String delete() throws IOException{
		if(product.getId()!=null){
			product = productService.get(product.getId());
			if(product.getImagePath()!=null){
				FileUploadUtil.deleteFile(product.getImagePath());
			}
		}
		productService.delete(product);

		putResponseText("删除成功");
		return NONE;
	}
	public String selectProductList() throws Exception{
		queryObject.setPageSize(20);
		putContext("brands",brandService.list());
		putContext(PAGERESULT, productService.query(queryObject));
		return "selectProductList";
	}
	
	

}
